%% ada

%% clean
clear all;
clc;
close all;
%% flag
showflag=true;
hold on
%% pos data
X=-1+2.*rand(1,50);
Y=-1+2.*rand(1,50);

szX=size(X,2);
pos=[];

for i=1:szX
    for j=1:szX
        if (X(i)*X(i)+Y(j)*Y(j))<1
            pos=[pos;[X(i),Y(j)]];
        end
    end
end

if showflag
    figure(1)
    plot(pos(:,1),pos(:,2),'k.');
end
%% neg data
X=-3+6.*rand(1,50);
Y=-3+6.*rand(1,50);
szX=size(X,2);
neg=[];
for i=1:szX
    for j=1:szX
        if (X(i)*X(i)+Y(j)*Y(j))>=1
            neg=[neg;[X(i),Y(j)]];
        end
    end
end
if showflag
    figure(1)
    plot(neg(:,1),neg(:,2),'b.');
end
%%
k=-50:0.5:50;
b=-20:0.5:20;

T=100
D=[1./(size(pos,1)+size(neg,1))];
Dt=[];
at=[];
Zt=[];
ert=[];




neg(:,3)=D;
pos(:,3)=D;


mine=100000;
features=[];

for t=1:T
    minrt=100000;
    miner=100000;
    for k=-50:0.5:50
        for b=-20:0.5:20
            
            rt=0.0;
            er=0.0;
            addflag=false;
            for i=1:size(pos,1)
                
                if k*pos(i,1)+b>pos(i,2)
                    rt =rt +pos(i,3);
                else
                    rt=rt -pos(i,3);
                    er=er + pos(i,3);
                    addflag=true;
                end
                
            end
            
            for i=1:size(neg,1)
                
                if k*neg(i,1)+b>neg(i,2)
                    rt =rt -neg(i,3);
                    er=er +neg(i,3);
                    addflag=true;
                else
                    rt=rt +neg(i,3);
                    
                end
                
                
            end
            if (er < miner&&addflag==true)
                minrt = rt;
                mink=k;
                minb=b;
                miner=er;
            end
            
            
            % break;
            %end
            
        end
    end
    at=[at,0.5*log((1+minrt)./(1-minrt))];
    ert=[ert,miner];
    Zt=[Zt,2*sqrt(miner*(1-miner))];
    features=[features;[mink,minb,miner,Zt(t),at(t)]];
    for i=1:size(pos,1)
        
        if mink*pos(i,1)+minb>pos(i,2)
            pos(i,3)=pos(i,3).*exp(-at(t))./Zt(t);
        else
            pos(i,3)=pos(i,3).*exp(at(t))./Zt(t);
            
        end
        
    end
    
    for i=1:size(neg,1)
        
        if mink*neg(i,1)+minb>neg(i,2)
            neg(i,3)=neg(i,3).*exp(at(t))./Zt(t);
        else
            neg(i,3)=neg(i,3).*exp(-at(t))./Zt(t);
        end
    end
    t
end

features

%%
hold off







